package edu.zut.cs.software.perfect.course.web.spring.controller;

import java.util.List;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

import edu.zut.cs.software.perfect.base.web.spring.controller.GenericController;
import edu.zut.cs.software.perfect.course.domain.CourseInformation;
import edu.zut.cs.software.perfect.course.service.CourseInformationManager;
/**
 * @ClassName CourseInformationController
 * @Description TODO
 * @Author liangying
 * @Date 2020年5月30日 上午12:20:39
 * @Version 1.0
 */

@ResponseBody
@Controller
@RequestMapping("/courseInformation")
public class CourseInformationController extends GenericController<CourseInformation, Long, CourseInformationManager> {

	CourseInformationManager courseInformationManager;

	/**
	 * The manager is injected into the controller
	 */
	@Autowired
	public void setCourseInformationManager(CourseInformationManager courseInformationManager) {
		this.courseInformationManager = courseInformationManager;
		this.manager = this.courseInformationManager;
	}
	
//	@GetMapping("/findAll/{page}/{size}")
//	public Page<CourseInformation> findall(@PathVariable("page")Integer page,@PathVariable("size")Integer size){
//		Pageable pageable=PageRequest.of(page-1,size);
//		return courseInformationManager.findAll(pageable);
//	}
//


	
//	@ResponseBody
//    @GetMapping(value = "hello", produces = "application/json;charset=utf-8")
//    public String hello() {
//        return "这里为课程的详细信息！";
//    }
	
	@ResponseBody
    @GetMapping(value = "findall", produces = "application/json;charset=utf-8")
	public String findAllCourseInformation() {
		List<CourseInformation> courseInformationList = this.manager.findAll();
        PageInfo pageInfo = new PageInfo(courseInformationList);
        return JSON.toJSONString(pageInfo);
	}
	
	@GetMapping("/findbyid/{id}")
	public String findById(@PathVariable("id") Long id) {
		Optional<CourseInformation> result = this.manager.findByCourseId(id);
		return JSON.toJSONString(result);
	}
	
//	@PostMapping("/save")
	@GetMapping("/save/{name}")
//	public CourseInformation save(@RequestBody CourseInformation courseInformation) {
	public CourseInformation save(@PathVariable("name") String courseName) {
		CourseInformation courseInformation = new CourseInformation();
		courseInformation.setCourseName(courseName);
		CourseInformation result = courseInformationManager.save(courseInformation);
		return result ;
	}
	
//	@DeleteMapping("/deletebyid/{id}")
	@GetMapping("/deletebyid/{id}")
	public Boolean deleteById(@PathVariable("id") Long id) {
//		Optional<CourseInformation> courseInformation = this.manager.findByCourseId(id);
		Boolean result = this.manager.deleteById(id);
		return result;
	}
	
	@GetMapping("/updata/{id}")
//	@PutMapping("/updata")
//	public CourseInformation updata(@RequestBody CourseInformation courseInformation) {
	public CourseInformation updata(@PathVariable("id") Long id) {
		Optional<CourseInformation> courseInformation = this.manager.findByCourseId(id);
		CourseInformation courseInformation_s = courseInformation.get();
		courseInformation_s.setCourseName("xiaoyingzi");
		CourseInformation result = this.manager.save(courseInformation_s);
		return result;
	}
	
	/**
	 * The request is mapped to the parent class
	 * @return
	 */
//	@RequestMapping(method = RequestMethod.GET, value = "/index.html")
//    public String index() {
//        return "/courseInformation/index";
//    }
	
	
	
}
